package com.yiren.dbaa.module.device.model;

import android.app.Activity;
import android.os.Handler;

import com.google.gson.Gson;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.CarSignalConfigBean;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.device.bean.DeviceDisplayInfoBean;
import com.yiren.dbaa.module.device.contract.DeviceInfoContract;
import com.yiren.dbaa.module.param_config.api.ParamConfigApi;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.SimCardInfoBean;
import com.yiren.dbaa.net.interf.StringCallback;
import com.yiren.dbaa.util.DecimalTransferUtil;
import com.yiren.dbaa.util.NetWorkUtil;
import com.yiren.dbaa.util.PhoneInfoUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * @author lwb
 * @date : 2022/7/6 15:17
 * @description:
 */
public class DeviceInfoModel implements DeviceInfoContract.Model {
    private Activity activity;
    private Gson json;
    private Handler timeHandler;

    public DeviceInfoModel(Activity activity) {
        this.activity = activity;
        json = new Gson();
        timeHandler = new Handler();
    }

    @Override
    public void getSimCard(CommonCallBack callBack) {
        PhoneInfoUtil phoneInfoUtil = new PhoneInfoUtil(activity);
        String iccid = phoneInfoUtil.getIccid();
        Map<String, Object> map = new HashMap<>();
        map.put("iccid", iccid);
        DeviceDisplayInfoBean displayInfoBean = new DeviceDisplayInfoBean();
        OkHttpUtils.getInstance().postJsonString(activity, AddressConstant.getSimCardInfoByIccid(), json.toJson(map), new StringCallback() {
            @Override
            public void getString(String result) {
                SimCardInfoBean simCardInfoBean = new Gson().fromJson(result, SimCardInfoBean.class);
                if (simCardInfoBean != null){
                    SimCardInfoBean.CardBean card1 = simCardInfoBean.getCard1();
                    if (card1 != null){
                        displayInfoBean.setSimType(card1.getSimName());
                        displayInfoBean.setTotalNetFlow(DecimalTransferUtil.readableFileSize(Double.parseDouble(card1.getSimTotalFlow()) * 1024 * 1024));
                        displayInfoBean.setRemainNetFlow(DecimalTransferUtil.readableFileSize(Double.parseDouble(card1.getSimUsableFlow()) * 1024 * 1024));
                        displayInfoBean.setActivateTime(card1.getServiceBeginTime());
                        displayInfoBean.setIccid(card1.getIccid());
                    }else {
                        displayInfoBean.setTotalNetFlow("");
                        displayInfoBean.setRemainNetFlow("");
                        displayInfoBean.setActivateTime("");
                        displayInfoBean.setIccid("N/A");
                        if (NetWorkUtil.isNetworkAvailable(activity)){
                            displayInfoBean.setSimType("Wifi");
                        }else {
                            displayInfoBean.setSimType("无网络");
                        }

                    }
                    callBack.success(displayInfoBean);
                }
            }

            @Override
            public void getFail() {
                callBack.fail("");
            }
        });


    }

    @Override
    public void refreshCarstate(CommonCallBack callBack) {
        callBack.success("");
        timeHandler.postDelayed(() -> refreshCarstate(callBack), 100);
    }

    @Override
    public void removeAllCallBack() {
        timeHandler.removeCallbacksAndMessages(null);
    }



}
